'use strict';
import {
    titileInputInit
} from '../components/titleInput.js';

var htmlModel = {
    "managerAdd": '<div id="managerAdd" class="weui-cell noborder"><div class="title_panel" id = "lodin_agency_panel" ><div class="weui-cell_hd"><label class="weui-label">机构号</label></div></div><div class="weui-cell__bd weui-cell_primary"><input id="lodin_agency" class="weui-input" type="number" placeholder="机构号"></div></div>'
}

var $verifyBtn, // 获取验证码按钮
    $lodinId, // 登录id输入框
    $lodinCard, // 登录密码
    $lodinAgency, // 登录机构号
    positionValue, // 登录角色
    $lodinBtn; // 等入按钮
var daojs = 60,
    isdux = true;
var MerchantNo, //上次登录的机构号
    staffPhone, //上次登录的手机号
    managerName, // 上次登录的客户经理名
    managerNo, // 上次登录的客户经理号
    opt_type; // 上次登录的类型

// 视图初始化
function viewInit() {
    initVale();
    bindOchanged();
    bindEvent();
    inputChangeEvent();
    forgotPasswordEvent();
    $verifyBtn.on("touchstart", smsSend); //发送短信
    setInfo();
}

// 初始化变量
function initVale() {
    $verifyBtn = $("#verifyBtn");
    $lodinId = $("#lodin_id");
    $lodinCard = $("#lodin_card");
    $lodinAgency = $("#lodin_agency");
    $lodinBtn = $("#lodin_btn");

    getLocalStorage();
    if (opt_type) {
        positionValue = opt_type;
        $("div[data-position]").removeClass("checked");
        $("div[data-position=" + opt_type + "]").addClass("checked");
        setLodinId(positionValue);
        showHidemanagerNo(positionValue)
        showHideVerifyBtn(positionValue);
    } else {
        positionValue = "1";
    }
}

// 绑定事件,这些事件中途会被卸载 重新绑定
function bindEvent() {
    titileInputInit();
    bindChangeRole();
    $("#lock-password").on("touchstart", changePasswordType);
    $("#clear-password").on("touchstart", function() {
        $lodinCard.val("");
    });
}

// 查看密码按钮的事件
function changePasswordType() {
    var passwordType = $lodinCard.attr("type");
    if (passwordType == "password") {
        if (positionValue == "2") {
            $lodinCard.attr("type", "tel");
        } else {
            $lodinCard.attr("type", "text");
        }
    } else {
        $lodinCard.attr("type", "password");
    }
}

// 设置header的高
function setHeaderH() {
    var $header = $("header");
    var cw = $header[0].clientWidth;
    var pt = parseInt(33 / 64 * cw) - 42;
    $header.css({
        "padding-top": pt + "px"
    });
}

// 转屏事件
function bindOchanged() {
    $(window).on("orientationchanged", function() {
        setHeaderH();
    });
}



// 切换角色事件绑定
function bindChangeRole() {

    $("header").on("touchstart", "div[data-position]", changePositon);
}

// 角色切换事件
function changePositon(e) {
    clearInput();
    checkUpInput();
    var $positions = $("div[data-position]");
    $("div[data-position]").removeClass("checked");
    $(this).addClass("checked");
    positionValue = $(this).attr("data-position");
    setLodinId(positionValue);
    showHidemanagerNo(positionValue);
    showHideVerifyBtn(positionValue);
    setInfo();
}

// 设置登录名名称
function setLodinId(positionValue) {
    var positionLables = {
        "1": "营业执照号",
        "0": "用户名",
        "2": "手机号"
    }
    var labelValue = positionLables[positionValue];
    $('#lodin_id_panel label').text(labelValue);
    $("#lodin_id").attr("placeholder", labelValue);

}

// 显示隐藏经理机构号
function showHidemanagerNo(positionValue) {
    if (positionValue == "0") {
        $(".weui-cell.noborder").removeClass("noborder");
        $("#managerAdd").css("display", "flex");

    } else {
        $("#lodin_id_panel").parent().addClass("noborder");
        $("#managerAdd").css("display", "none");
    }
}

// 显示隐藏 获取验证码按钮
function showHideVerifyBtn(positionValue) {
    if (positionValue == "2") {
        $("#verifyBtn_box").css("display", "block");
        $("#lodin_card").attr("placeholder", "验证码");
        $("#lodin_card_panel label").text("验证码");
        $("#forgotPassword").parent().hide();
        $("#open_btn").hide();
    } else {
        $("#verifyBtn_box").css("display", "none");
        $("#lodin_card").attr("placeholder", "登录密码");
        $("#lodin_card_panel label").text("登录密码");

        if (positionValue == "0") {
            $("#open_btn").hide();
            $("#forgotPassword").parent().hide();
        } else {
            $("#forgotPassword").parent().show(); // 需求待定
            $("#open_btn").show();
        }
    }
}

// 事件解绑
function offEvent() {
    $("header").off("touchstart", "div[data-position]");
    $(".weui-cells_form input").off("touchstart");
    $(".weui-cells_form input").off("blur");
}

// 清空input
function clearInput() {
    $(".weui-cells_form input").val("");
    $(".weui-cells_form input").trigger("blur");
}

// 绑定账户\密码\机构输入框输入事件
function inputChangeEvent() {
    $lodinId.on("input", function() {
        checkUpInput();
        checkVerify();
    });
    $lodinCard.on("input", checkUpInput);
    $lodinAgency.on("input", checkUpInput);
}

// 验证账户\密码\机构是否为空 允许登录
function checkUpInput() {
    var checkedVal = true;
    checkedVal = checkLodinName(checkedVal);
    checkedVal = checkedPassword(checkedVal);
    checkedVal = checkedAgencyNo(checkedVal);
    if (checkedVal) {
        $lodinBtn.attr("class", "weui-btn weui-btn_primary");

    } else {
        $lodinBtn.attr("class", "weui-btn weui-btn_disabled weui-btn_default ui-link");
    }
}

// 验证登录名
function checkLodinName(checkedVal) {
    var lodinVal = $lodinId.val();

    if (lodinVal == "") { // 验证登录名不能为空
        checkedVal = false;
    } else if (positionValue == "2") { // 营业员手机号必须11位才允许登录
        if (lodinVal.length > 11) {
            lodinVal = lodinVal.substr(0, 11);
            $lodinId.val(lodinVal);
        }
        if (lodinVal.length != 11) {
            checkedVal = false;
        }

    }

    return checkedVal;
}

// 验证登录密码
function checkedPassword(checkedVal) {
    var passwordVal = $lodinCard.val();
    if (passwordVal == "") { // 验证密码验证码
        checkedVal = false;
    } else if (positionValue == "2") { // 营业员验证码必须4位
        if (passwordVal.length > 4) {
            passwordVal = passwordVal.substr(0, 4);
            $lodinCard.val(passwordVal);
        }
        if (passwordVal.length != 4) {
            checkedVal = false;
        }
    }

    return checkedVal;
}

// 验证客户经理机构号
function checkedAgencyNo(checkedVal) {
    if (positionValue == "0" && $lodinAgency.val() == "") {
        checkedVal = false;
    }
    return checkedVal;
}

// 判断是否允许发送验证码
function checkVerify() {
    var lodinVal = $lodinId.val();

    if (lodinVal.length == 11 && positionValue == "2" && daojs == 60) {
        $verifyBtn.attr("class", "weui-btn weui-btn_plain-primary");
    } else {
        $verifyBtn.attr("class", "weui-btn weui-btn_plain-default weui-btn_plain-disabled");
    }
}

// 取发送短信参数
function getsmsParameters() {
    var parameters = {};
    parameters.lodinId = $("#lodin_id").val();
    parameters.position = $("header .weui-flex__item.checked").attr("data-position");
    parameters.sendType = "1"; //发送类型
    //获取url中的openid
    parameters.openid = openid;
    return parameters;
}


// 短信发送
function smsSend() {

    var positionValue = $("header .weui-flex__item.checked").attr("data-position");
    if ($verifyBtn.hasClass("weui-btn_plain-disabled")) {
        return;
    } else if (isdux) {
        isdux = false;
        djsfunc();
        var parameters = getsmsParameters();
        $.post(duanxin_URL, parameters, smsCallBack);
    }

}

// 验证码倒计时
function djsfunc() {
    var handle = setInterval(function() {
        $verifyBtn.text("重新获取(" + daojs + "S)");
        $verifyBtn.attr("class", "weui-btn weui-btn_plain-default weui-btn_plain-disabled");
        daojs--;
        if (daojs == 0) {
            clearInterval(handle);
            $verifyBtn.text("重新获取验证码");
            daojs = 60;
            isdux = true;
            $verifyBtn.attr("class", "weui-btn weui-btn_plain-primary");
        }
    }, 1000);
}

//短信发送 回调函数
function smsCallBack(msg) {

    console.log("msg");
    console.dir(msg);
    if (msg.code != "00" && msg.code != "2") {
        errorObject.showErroDialog(msg.msg);
    }

}

// 忘记密码跳转
function forgotPasswordEvent() {
    $("#forgotPassword").on("click", function() {
        location.href = "./forgotPassword.html?role=merchant";
    });
}

// 设置上次登录信息
function setInfo() {
    if (positionValue == "1") {
        $lodinId.attr("type", "text");
        if (MerchantNo) {
            $lodinId.val(MerchantNo);
        }

    } else if (positionValue == "2") {
        $lodinId.attr("type", "tel");
        if (staffPhone) {
            $lodinId.val(staffPhone);
            $verifyBtn.attr("class", "weui-btn weui-btn_plain-primary");
        }

    } else if (positionValue == "0") {
        $lodinId.attr("type", "text");
        if (managerNo) {
            $lodinAgency.val(managerNo);
        }
        if (managerName) {
            $lodinId.val(managerName);
        }
    }
}

// 取上次登录信息
function getLocalStorage() {
    MerchantNo = localStorage.getItem("merchantNo");
    staffPhone = localStorage.getItem("staffPhone");
    managerName = localStorage.getItem("managerName");
    managerNo = localStorage.getItem("managerNo");
    opt_type = localStorage.getItem("opt_type");
}



export {
    setHeaderH,
    viewInit
}